package com.ruoyi.web.controller.bussiness;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PatrolAreaBo;
import com.ruoyi.system.domain.vo.PatrolAreaVo;
import com.ruoyi.system.service.IPatrolAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 巡区管理
 *
 * @author ruoyi
 * @date 2024-01-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/patrolArea")
public class PatrolAreaController extends BaseController {

    private final IPatrolAreaService patrolAreaService;

    /**
     * 查询巡区管理列表
     */
    @SaCheckPermission("system:patrolArea:list")
    @GetMapping("/list")
    public TableDataInfo<PatrolAreaVo> list(PatrolAreaBo bo, PageQuery pageQuery) {
        return patrolAreaService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡区管理列表
     */
    @SaCheckPermission("system:patrolArea:export")
    @Log(title = "巡区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatrolAreaBo bo, HttpServletResponse response) {
        List<PatrolAreaVo> list = patrolAreaService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡区管理", PatrolAreaVo.class, response);
    }

    /**
     * 获取巡区管理详细信息
     *
     * @param areaId 主键
     */
    @SaCheckPermission("system:patrolArea:query")
    @GetMapping("/{areaId}")
    public R<PatrolAreaVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long areaId) {
        return R.ok(patrolAreaService.queryById(areaId));
    }

    /**
     * 新增巡区管理
     */
    @SaCheckPermission("system:patrolArea:add")
    @Log(title = "巡区管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatrolAreaBo bo) {
        return toAjax(patrolAreaService.insertByBo(bo));
    }

    /**
     * 修改巡区管理
     */
    @SaCheckPermission("system:patrolArea:edit")
    @Log(title = "巡区管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatrolAreaBo bo) {
        return toAjax(patrolAreaService.updateByBo(bo));
    }

    /**
     * 删除巡区管理
     *
     * @param areaIds 主键串
     */
    @SaCheckPermission("system:patrolArea:remove")
    @Log(title = "巡区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                         @PathVariable Long[] areaIds) {
        return toAjax(patrolAreaService.deleteWithValidByIds(Arrays.asList(areaIds), true));
    }
}
